package com.lz.ionicbase.widget;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.widget.Toast;

import com.lz.ionicbase.R;
import com.lz.ionicbase.ui.activity.SetServerActivity;


public class Notice {

    public static void alert(Context context, String message) {
        alert(context, message, null);
    }

    public static void alert(Context context, String message,
                             final OnClickListener listener) {
        new AlertDialog.Builder(context).setTitle("提示").setMessage(message)
                .setPositiveButton("确定", listener).show();
    }

    public static void alert(Context context, String message,
                             String buttonText, final OnClickListener listener) {
        new AlertDialog.Builder(context).setTitle("提示").setMessage(message)
                .setPositiveButton(buttonText, listener).show();
    }

    public static void alert(Context context, String message,
                             String buttonLeft, final OnClickListener listenerL, String buttonRight, final OnClickListener listenerR) {
        new AlertDialog.Builder(context).setTitle("提示").setMessage(message)
                .setPositiveButton(buttonLeft, listenerL)
                .setNegativeButton(buttonRight, listenerR).show();
    }

    public static void alertAndFinish(final Context context, String message) {
        new AlertDialog.Builder(context).setTitle("提示").setMessage(message)
                .setPositiveButton("确定", new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        ((Activity) context).finish();
                    }
                }).show();
    }

    public static void confirm(Context context, String message,
                               final OnClickListener yesListener,
                               final OnClickListener cancelListener) {
        new AlertDialog.Builder(context).setTitle("提示").setMessage(message)
                .setPositiveButton("确定", yesListener)
                .setNegativeButton("取消", cancelListener).show();
    }


    public static void showSnackbar(Context context,View container, String msg) {
        Snackbar snackbar = Snackbar.make(container, msg, Snackbar.LENGTH_LONG);
        snackbar.getView().setBackgroundResource(R.color.app_base);
        snackbar.show();
    }

    public static void showSnackbar(Context context,View container, String msg, @Snackbar.Duration int duration) {
        Snackbar snackbar = Snackbar.make(container, msg, duration);
        snackbar.getView().setBackgroundResource(R.color.app_base);
        snackbar.show();
    }


    /**
     * 数据异常
     * @param context
     */
    public static void dataException(Context context) {
        Toast.makeText(context,"数据异常",Toast.LENGTH_LONG).show();
    }
    /**
     * 提醒是否需要设置服务器
     * @param snContainer
     */
    public static void noNetWork(Context context, View snContainer) {
        try {
            if(snContainer == null){
                snContainer = ((Activity)context).findViewById(R.id.coordinator);
                if(snContainer == null){
                    snContainer = ((Activity)context).findViewById(android.R.id.content);
                }
            }
            if(snContainer != null){
                showSnackbar(context, snContainer, "网络连接不可用!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 提醒是否需要设置服务器
     * @param context
     */
    public static void alertSetServer(final Context context) {

        try {
            new AlertDialog.Builder(context).setTitle("提示").setMessage("网络连接失败，是否需要检查服务器设置？")
                    .setPositiveButton("确定", new OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            Intent intent = new Intent(context, SetServerActivity.class);
                            context.startActivity(intent);
                        }
                    })
                    .setNegativeButton("取消",null).show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
